<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="../res/css/weui.css" />
    <link rel="stylesheet" href="../res/css/weuix.css" />
    <link rel="stylesheet" href="../res//css/common.css">
    <link rel="stylesheet" href="../res/css/swiper.css">
    <script src="../res/js/zepto.min.js"></script>
    <script src="../res/js/zepto.weui.js"></script>
    <script src="../res/js/common.js?v=1"></script>
    <script src="../res/js/swiper.js"></script>
    <script src="../res/js/socket.js"></script>
</head>
<style>
    .hide {
        display: none;
    }

    .fixed-top {
        border-bottom: 1px solid #e8e8e8;
        position: fixed;
        left: 0;
        top: 0;
        right: 0;
        z-index: 99;
        background: #F6F6F9
    }

    .navbar {
        width: 70%;
        display: flex;
        margin: 20px 5%;
        margin-top: 70px;
        height: 20px;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #757575;
        line-height: 20px;
    }

    .choose {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #2C2C2C;
    }

    .weui-cell {
        width: 90%;
        height: 25px;
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #757575 !important;
    }

    .doctor_message {
        width: 95%;
        margin: 20px 2.5%;
        background: #FFFFFF;
        box-shadow: 0px 8px 18px 0px rgba(221, 219, 219, 0.2);
        border-radius: 10px;
        overflow: hidden;
    }

    .weui-select {
        padding: 0 !important;
    }

    #name {
        width: 100%;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #353535;
    }

    #message {
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #2C2C2C;
    }

    .recently_waiting {
        /* width: 90%; */
        margin: 10px 5%;
        background: #FBFBFB;
        border-radius: 10px;
        overflow: hidden;
    }

    .recently_waiting div {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: black;
        margin: 10px 5%;
        display: flex;
    }

    .recently_waiting span {
        display: inline-block;
        width: 30%;
        color: #757575;
        margin: 0 3px;
        margin-right: 10px;
    }


    #unreadIcon {
        height: 8px;
        width: 8px;
        background-color: red;
        border-radius: 50%;
        position: absolute;
        right: 40px;
    }

    .noFind {
        width: 90%;
        margin: 10px 2.5%;
        display: block;
        text-align: center;
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #353535;
        background-color: #F9F8FE;
    }

    .noFind img {
        width: 198px;
        height: 158px;
        border: none;
        border-radius: 0px;
        margin: 15px;
    }
</style>
<script>
    $(function () {
        //tabbar跳转
        function tabbargo(openid, orgid) {
            $('.weui-tabbar__item').on('click', function () {
                var type = $(this).attr("id");
                console.log(type);
                if (type == "inquiry_index") {
                    location.href = "inquiry_doctor_index.htm?orgid=" + orgid + "&openid=" + openid
                } else if (type == "inquiry_doctor") {
                    if (!isLogin) {
                        $.confirm("请先登录再操作", function () {
                            goPage('login.htm');
                        }, function () {
                            //取消操作
                        });
                    } else {
                        location.href = "inquiry_doctor.htm?orgid=" + orgid + "&openid=" + openid
                    }
                } else if (type == "my_doctor_index") {
                    if (!isLogin) {
                        $.confirm("请先登录再操作", function () {
                            goPage('login.htm');
                        }, function () {
                            //取消操作
                        });
                    } else {
                        location.href = "my_doctor_index.htm?orgid=" + orgid + "&openid=" + openid
                    }
                } else {
                    location.href = "inquiry_user_personal.htm?orgid=" + orgid + "&openid=" + openid;
                }
            });
        }

        //获取Url参数
        var openid = getQst("openid").trim();
        var orgid = getQst("orgid").trim();
        tabbargo(openid, orgid);
        //医生列表渲染函数
        function doctor_info(json) {
            if (json.data.length == 0) {
                var doctorMessage = "";
                doctorMessage = `<div>
		        </div>
		        <div class="weui-cell noFind"><img src="../res/images/img_empty_state@2x.png" alt="">
			        <p>抱歉，查无关于此结果的数据~</p>
		        </div>
	            </div>`
                // console.log(doctorMessage);
                $(".doctor_wrappar").html(doctorMessage)
                $.hideLoading();
                clickEvent()
            } else {
                var doctorMessage = "",
                    patientSex = "",
                    classHide = "hide"
                json.data.forEach((item, index) => {
                    item.patientSex == 1 ? patientSex = "男" : patientSex = "女"
                    item.msg == 0 ? classHide = "hide" : classHide = ""

                    doctorMessage += `<div class="doctor_message" id=${item.doctorId}>
                    <div style="margin:20px 5%;display: flex;" id=${item.doctorId}>
                    <div class="expertlist_map_img" style="margin-right:5%"> <img src=${item.doctorHeadImg} /> </div>
                    <div style="display:flex;flex-wrap:wrap;">
                        <p id="name">${item.trueName}</p>
                        <p id="message">${item.doctorJobTitle}&nbsp|&nbsp${item.departmentName ? `${item.departmentName}&nbsp|` :  ''}&nbsp从业${item.workAge}年</p>
                    </div>`
                    //isConcern是否关注 0和null为不关注，1关注 
                    if (item.isConcern === null || item.isConcern == 0) {
                        doctorMessage +=
                            `<img style="width: 25px;margin-left: 20%;" src="../res/images/icon_follow.png" alt="" class="isStar" isStar=0 isConcern=${item.isConcern} doctorId=${item.doctorId}>`
                    } else {
                        doctorMessage +=
                            `<img style="width: 25px;margin-left: 20%;" src="../res/images/icon_follow_select.png" alt="" class="isStar" isStar=1 isConcern=${item.isConcern} doctorId=${item.doctorId}>`
                    }
                    doctorMessage += `</div>
                    <div class="recently_waiting">`
                    if (item.logId == null) {
                        doctorMessage += `
                            <div><span>最近问诊人:</span></div>
                            <div><span>咨 询 日 期:</span></div>
                        </div>
                        <div style="width: 84%;margin: 15px 8%;font-size: 13px;font-family: PingFang SC;font-weight: 500;color: #2C2C2C;width:30%"" id=${item.logId} class="immediatelyAsk">
                        立即问诊></div>
                        </div>`
                    } else {
                        doctorMessage += `
                            <div><span>最近问诊人:</span>${item.patientName} ${item.patientAge}岁 ${patientSex}</div>`
                        if (item.diagnosisDescribe) {
                            doctorMessage +=
                                `<div><span style="width:30%;">诊&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 断:</span><span style="width:70%;color:black;margin-left:8px">${item.diagnosisDescribe}</span></div>`
                        } else {

                        }
                        doctorMessage += `<div><span>咨 询 日 期:</span>${item.createTime}</div>
                            </div>
                             <div style="width: 84%;margin: 15px 8%;font-size: 13px;font-family: PingFang SC;font-weight: 500;color: #2C2C2C;">
                            <p class="goListPage" inquiryPatientId=${item.inquiryPatientId} logId=${item.logId} style="width:30%">清单 ></p></div>
                            <div style="width: 84%;margin: 15px 8%;font-size: 13px;font-family: PingFang SC;font-weight: 500;color: #2C2C2C;width:30%">
                            <div class="recording" style="width:300px"  doctorId=${item.doctorId} doctorIm=${item.doctorIm} userIm=${item.userIm} inquiryPatientId=${item.inquiryPatientId} logId=${item.logId}>问诊对话记录 ><span style="color: #1189FF;margin-left: 40%; id="msg" doctorId=${item.doctorId} class=${classHide}>${item.msg}条新留言</span></div>
                            </div>
                            <div style="width: 84%;margin: 15px 8%;font-size: 13px;font-family: PingFang SC;font-weight: 500;color: #2C2C2C;width:30%"" id=${item.logId} class="immediatelyAsk">
                            立即问诊></div>
                            </div>`
                    }
                })
                $(".doctor_wrappar").html(doctorMessage)
                $.hideLoading();
                clickEvent()
            }
        }

        //获取我的医生列表
        function getMyDoctorList() {
            newAjaxPost("/h5/myDoctor/getMyDoctorList", {}, getMyDoctorListOk);
        }

        function getMyDoctorListOk(json) {
            console.log(json);
            doctor_info(json)
        }


        var fromDoctorPage = getQst("isMydoctorPage")
        if (fromDoctorPage) {
            $("#getMyDoctorList").addClass('choose').siblings('div').removeClass()
            $.showLoading()
            getMyDoctorList()
            $(".picker").show()
            // takePicker()
            // clickEvent("getMyDoctorList")
        } else {
            $.showLoading();
            getFamilyDoctorList()
            $(".picker").hide()
        }

        //建立webscoket连接
        //初始化webscoket
        // function initWebSocket() {
        //     // 连接socket 重构的聊天要用到socket
        //     var token = null;
        //     var userJson = localStorage.getItem('DSXBuserJson');
        //     user_id = strToJson(userJson).id
        //     if (userJson) {
        //         token = strToJson(userJson).h5Token
        //     }
        //     wsConnection.initWebSocket('wss://hospital-test.yixianmedical.com/websocket', token);
        // }
        // initWebSocket()

        //获取家庭医生
        function getFamilyDoctorList() {
            newAjaxPost("/h5/myDoctor/getFamilyDoctorList", {}, getFamilyDoctorListOk);
        }

        //患者加入聊天室通知对方
        function noticeDoctor(patientIM, doctor_imid, logId) {
            wsConnection.send(38, {
                userIm: patientIM, //发送方im
                friendIm: doctor_imid, //接收方im
                logId: logId
            });
        }


        function getFamilyDoctorListOk(json) {
            // console.log(json);
            doctor_info(json)
        }

        //请求获取付费信息
        function newGetConsultationFee(doctorId, fn) {
            var params = {
                "doctorId": doctorId,
            }
            newAjaxPost("/h5/wxpay/getConsultationFee", params, fn);
        }

        //设置点击事件
        function clickEvent(type) {
            $(".isStar").on('click',function (e) {
                e.stopPropagation()
                //id=0为没收藏，1为收藏
                if ($(this).attr("isStar") == 0) {
                    $(this).attr('src', "../res/images/icon_follow_select.png")
                    $(this).attr("isStar", 1)
                    $(this).attr("isConcern", 1)
                    concernDoctor($(this).attr("doctorId"), 1)
                } else {
                    //请求获取付费信息,判断是否还在家庭问诊中
                    doctorId = $(this).attr("doctorId")
                    // console.log(doctorId);
                    newGetConsultationFee(doctorId, (jsonInfo) => {
                        // console.log(jsonInfo);
                        data = jsonInfo.data
                        if (data && data.inquiryType == 3) {
                            msgInfo("当次问诊仍未结束，请结束后再尝试")
                        } else {
                            $(this).attr('src', "../res/images/icon_follow.png")
                            $(this).attr("isStar", 0)
                            $(this).attr("isConcern", 0)
                            concernDoctor($(this).attr("doctorId"), 0)
                        }
                    })
                }
            })

            //跳转到清单页面
            $(".goListPage").on('click',function (e) {
                e.stopPropagation()
                console.log(e)
                let logId = $(this).attr("logId")
                goPage(`my_prescription_list.htm?logId=${logId}`);
            })

            //跳转医生详细信息页面
            $(".doctor_message").on('click',function () {
                var doctor_id = $(this).attr("id")
                var isMydoctorPage = $("#getMyDoctorList").hasClass('choose')
                goPage(`doctor_home_page.htm?doctor_id=${doctor_id}&isMydoctorPage=${isMydoctorPage}`)
            })

            //跳转立即问诊
            $(".immediatelyAsk").on('click',function (e) {
                e.stopPropagation()
                goPage('doctor_home_page.htm?doctor_id=' + $(this).parent().attr("id"))
            })

            //跳转到对话记录页面(判断是否结束问诊和是否填写玩问卷)
            $(".recording").on('click',function (e) {
                e.stopPropagation()
                doctorId = $(this).attr("doctorId") //医生id
                doctorIm = $(this).attr("doctorIm") //医生im
                userIm = $(this).attr("userIm") //用户im
                inquiryPatientId = $(this).attr("inquiryPatientId") //问诊患者信息主键id
                //获取医生当前订单信息
                newGetConsultationFee(doctorId, (jsonInfo) => {
                    console.log(jsonInfo);
                    data = jsonInfo.data //请求到的data
                    //判断是否结束问诊
                    if (data) {
                        userJson = getUserInfo() //获取用户信息相关
                        h5token = userJson.h5Token
                        userid = userJson.id;
                        usertype = userJson.type;
                        inquiryPatientId = data.inquiryPatientId //问诊人信息id
                        doctor_id = data.doctorId //医生Id
                        doctorIm = data.doctorIm //医生Im
                        userIm = data.userIm //用户Im
                        doctor_name = data.trueName //医生姓名
                        inquiryType = data.inquiryType //问诊类型
                        if (inquiryType == 1 || inquiryType == 2) {
                            let logId = $(this).attr("logid")
                            strGoUrl =
                                `chat_record.htm?userIm=${userIm}&doctorIm=${doctorIm}&logId=${logId}`
                            goPage(strGoUrl)
                        } else {
                            //判断是否填写完问卷,logId不为Null,则填好问卷
                            if (data.logId) {
                                noticeDoctor(userIm, doctorIm, data.logId)
                                strGoUrl = 'chat.htm?isChatWait=0&log_id=' + data.logId +
                                    '&doctor_id=' + doctor_id +
                                    '&doctor_imid=' +
                                    doctorIm + '&log_type=' + inquiryType +
                                    '&inquirypatientId=' +
                                    data.inquiryPatientId + '&patientIM=' + userIm +
                                    '&fromMyDoctor=1'
                                goPage(strGoUrl)
                            } else {
                                strGoUrl = 'inquiry_doctor_patient.htm?doctor_id=' + doctor_id +
                                    '&doctor_name=' + doctor_name + '&log_type=' + inquiryType +
                                    '&h5_token=' + h5token + '&user_id=' + userid +
                                    '&user_type=' +
                                    usertype;
                                goPage(strGoUrl);
                            }
                        }
                    } else {
                        let logId = $(this).attr("logid")
                        strGoUrl =
                            `chat_record.htm?userIm=${userIm}&doctorIm=${doctorIm}&logId=${logId}`
                        goPage(strGoUrl)
                        console.log("问诊结束");
                        console.log(doctorId);
                        console.log(logId);
                    }
                })
            })

            //筛选
            $("#mobile").picker({
                title: "请选择问诊类型",
                cols: [{
                    textAlign: 'center',
                    values: ['全部', '视频问诊', '图文问诊', '家庭医生', '复诊']
                }],
                onChange: function (p, v, dv) {
                    // console.log(p);
                    // console.log(v);
                    // console.log(dv);
                },
                onClose: function (p, v, d) {
                    console.log(p.cols[0].activeIndex);
                    if (p.cols[0].activeIndex == 0) {
                        var params = {}
                    } else {
                        var params = {
                            "inquiryType": p.cols[0].activeIndex
                        }
                    }
                    newAjaxPost("/h5/myDoctor/getMyDoctorList", params, (json) => {
                        console.log(json);
                        doctor_info(json)
                    });
                }
            });
        }
        // takePicker()
        // clickEvent()
        //关注或取消关注
        function concernDoctor(doctorId, isConcern) {
            console.log(...arguments)
            var params = {
                "doctorId": doctorId,
                "isConcern": isConcern
            }
            newAjaxPost("/h5/myDoctor/concernDoctor", params, concernDoctorOk);
        }

        function concernDoctorOk(json) {
            msgInfo(json.msg);
        }

        //选项卡切换
        $(".navbar").find("div").click(function () {
            $(this).addClass('choose').siblings('div').removeClass('choose');
            $.showLoading();
            if ($(this).attr("id") == "getMyDoctorList") {
                getMyDoctorList()
                $(".picker").show()
                // takePicker()
                // clickEvent("getMyDoctorList")
            } else {
                getFamilyDoctorList()
                $(".picker").hide()
                // takePicker()
                // clickEvent("getFamilyDoctor")
            }
        })

        //筛选框刷新
        // function takePicker() {
        //     $(".picker").html(`
        //         <div class="weui-cell weui-cell_select">
        //             <div class="weui-cell__bd">
        //                 <input disabled class="weui-select" id="mobile" type="text" value="全部" style="padding-left: 20px;">
        //             </div>
        //         </div>`)
        // }
        //我的医生未读消息小红点显示
        wsConnection.onMessage = function (msg) {
            if (msg.packageType == 40) {
                // console.log(msg.isRead);
                //是否有未读消息0为不显示1为显示
                msgIsRead = msg.isRead
                if (msgIsRead == 0) {

                } else {
                    $("#unreadIcon").show()
                }
            }
        }
    })
</script>


<body>
    <div class="fixed-top">
        <div class="weui-btn_primary weui-header">
            <div class="weui-header-left"><a class="icon icon-109" onclick="history.back()"></a></div>
            <h1 class="weui-header-title">我的医生</h1>
        </div>
    </div>
    <!-- 选项卡 -->
    <div class="navbar">
        <div class="choose" id="getFamilyDoctorLis">家庭医生</div>
        <div style="margin-left:10%" id="getMyDoctorList">我的医生</div>
    </div>
    <!-- picker -->
    <div class="picker">
        <div class="weui-cell weui-cell_select">
            <div class="weui-cell__bd">
                <input class="weui-select" id="mobile" type="text" value="全部" style="padding-left: 20px;">
            </div>
        </div>
    </div>
    <!-- 医生资料卡 -->
    <div class="doctor_wrappar">
    </div>
    <div style="height: 50px;">&nbsp;</div>
    <div id='tb' class="weui-tab" style="height:auto;">
        <div class="weui-tabbar tab-bottom">
            <a href="javascript:;" class="weui-tabbar__item" id="inquiry_index">
                <span style="display: inline-block;position: relative;">
                    <img src="../res/images/acitve_index.png" alt="" class="weui-tabbar__icon">
                </span>
                <p class="weui-tabbar__label">首页</p>
            </a>
            <a href="javascript:;" class="weui-tabbar__item" id="inquiry_doctor">
                <img src="../res/images/acitve_wz.png" alt="" class="weui-tabbar__icon">
                <p class="weui-tabbar__label">问诊</p>
            </a>
            <a href="javascript:;" class="weui-tabbar__item" id="my_doctor_index">
                <span style="display: inline-block;position: relative;">
                    <img src="../res/images/icon_doctor_select.png" class="weui-tabbar__icon">
                </span>
                <span id="unreadIcon" style="display: none;"></span>
                <p class="weui-tabbar__label ">我的医生</p>
            </a>
            <a href="javascript:;" class="weui-tabbar__item" id="inquiry_user_per">
                <img src="../res/images/active_per.png" class="weui-tabbar__icon">
                <p class="weui-tabbar__label">我的</p>
            </a>
        </div>
    </div>
</body>

</html>